"use strict";
cc._RF.push(module, '8e845wSi+tBkaqwXVo1rtlK', 'SimpleGameEventHandler');
// scripts/game/SimpleGameEventHandler.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleGameEventHandler = void 0;
var GameBean_1 = require("../bean/GameBean");
var EventCenter_1 = require("../common/EventCenter");
var GameMgr_1 = require("../common/GameMgr");
/**
 * 简化的游戏事件处理器
 * 专门处理方块动画相关的GameFrame消息
 */
var SimpleGameEventHandler = /** @class */ (function () {
    function SimpleGameEventHandler() {
        this.gamePageController = null;
    }
    /**
     * 设置游戏页面控制器
     */
    SimpleGameEventHandler.prototype.setGamePageController = function (controller) {
        this.gamePageController = controller;
    };
    /**
     * 注册WebSocket消息处理器
     */
    SimpleGameEventHandler.prototype.registerWebSocketHandlers = function () {
        GameMgr_1.GameMgr.Event.AddEventListener(EventCenter_1.EventType.ReceiveMessage, this.onReceiveMessage, this);
    };
    /**
     * 处理接收到的WebSocket消息
     */
    SimpleGameEventHandler.prototype.onReceiveMessage = function (messageBean) {
        // 这里不再处理 GameFrame，避免与 GamePageController.handleGameFrame 重复驱动
        if (messageBean.msgId === "GameFrame") {
            return;
        }
        // 其他消息类型按需扩展
    };
    /**
     * 处理GameFrame消息
     */
    SimpleGameEventHandler.prototype.handleGameFrame = function (frameData) {
        if (!frameData) {
            console.error("[SimpleGameEventHandler] frameData为空");
            return;
        }
        if (!frameData.gameEvents) {
            return;
        }
        if (!Array.isArray(frameData.gameEvents)) {
            console.error("[SimpleGameEventHandler] gameEvents不是数组:", frameData.gameEvents);
            return;
        }
        // 处理每个游戏事件
        for (var i = 0; i < frameData.gameEvents.length; i++) {
            var gameEvent = frameData.gameEvents[i];
            this.processGameEvent(gameEvent);
        }
    };
    /**
     * 处理单个游戏事件
     */
    SimpleGameEventHandler.prototype.processGameEvent = function (gameEvent) {
        if (!gameEvent) {
            console.error("[SimpleGameEventHandler] gameEvent为空");
            return;
        }
        var anyEvent = gameEvent;
        var eventType = anyEvent.eventType;
        // 兼容两种结构：{ eventType, eventData: {...} } 或 { eventType, blocks, countDown, ... }
        var eventData = anyEvent.eventData || anyEvent;
        // 只处理方块相关的事件类型 (1-6)
        if (eventType >= 1 && eventType <= 6) {
            this.handleBlockEvent(eventType, eventData);
        }
        else {
        }
    };
    /**
     * 处理方块相关事件
     */
    SimpleGameEventHandler.prototype.handleBlockEvent = function (eventType, eventData) {
        // 支持 blockChanges 与 blocks 两种字段
        var blocksSource = eventData && (eventData.blockChanges || eventData.blocks);
        // 构造标准的GameEventData
        var gameEventData = {
            eventType: eventType,
            currentRound: (eventData && eventData.currentRound) || 1,
            countDown: (eventData && eventData.countDown) || 0,
            randomSeed: (eventData && eventData.randomSeed) || Math.floor(Math.random() * 100000),
            message: this.getEventMessage(eventType),
            blocks: this.parseBlockChanges(blocksSource)
        };
        // 转发给GamePageController
        this.forwardToController(gameEventData);
    };
    /**
     * 解析方块变化数据
     */
    SimpleGameEventHandler.prototype.parseBlockChanges = function (blockChanges) {
        if (!blockChanges || !Array.isArray(blockChanges)) {
            return [];
        }
        return blockChanges.map(function (block, index) { return ({
            id: index + 1,
            x: block.x || 0,
            y: block.y || 0,
            blockType: 1,
            animationState: GameBean_1.BlockAnimationState.NONE,
            animationDelay: 0
        }); });
    };
    /**
     * 获取事件消息
     */
    SimpleGameEventHandler.prototype.getEventMessage = function (eventType) {
        switch (eventType) {
            case 1: return "游戏等待中";
            case 2: return "回合等待中";
            case 3: return "方块开始松动";
            case 4: return "方块开始掉落";
            case 5: return "方块开始补齐";
            case 6: return "游戏结束";
            default: return "未知事件";
        }
    };
    /**
     * 转发事件到控制器
     */
    SimpleGameEventHandler.prototype.forwardToController = function (gameEventData) {
        if (!this.gamePageController) {
            console.error("[SimpleGameEventHandler] GamePageController未设置，无法处理游戏事件");
            return;
        }
        this.gamePageController.handleGameEvent(gameEventData);
    };
    /**
     * 创建测试事件
     */
    SimpleGameEventHandler.prototype.createTestShakingEvent = function () {
        var testEvent = {
            eventType: GameBean_1.GameEventType.SHAKING,
            currentRound: 1,
            countDown: 2000,
            randomSeed: 12345,
            message: "测试方块松动",
            blocks: [
                { id: 1, x: 2, y: 3, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 },
                { id: 2, x: 4, y: 5, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 },
                { id: 3, x: 6, y: 7, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 }
            ]
        };
        this.forwardToController(testEvent);
    };
    /**
     * 创建测试掉落事件
     */
    SimpleGameEventHandler.prototype.createTestFallingEvent = function () {
        var testEvent = {
            eventType: GameBean_1.GameEventType.FALLING,
            currentRound: 1,
            countDown: 500,
            randomSeed: 54321,
            message: "测试方块掉落",
            blocks: [
                { id: 1, x: 1, y: 1, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 },
                { id: 2, x: 3, y: 4, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 },
                { id: 3, x: 7, y: 8, blockType: 1, animationState: GameBean_1.BlockAnimationState.NONE, animationDelay: 0 }
            ]
        };
        this.forwardToController(testEvent);
    };
    /**
     * 清理资源
     */
    SimpleGameEventHandler.prototype.cleanup = function () {
        if (this.gamePageController) {
            GameMgr_1.GameMgr.Event.RemoveEventListener(EventCenter_1.EventType.ReceiveMessage, this.onReceiveMessage, this);
            this.gamePageController = null;
        }
    };
    return SimpleGameEventHandler;
}());
exports.SimpleGameEventHandler = SimpleGameEventHandler;

cc._RF.pop();